package com.example.demo.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

@RestController
@RequestMapping("/user")
@Api(tags = {"用户操作操作接口"}, description = " ")
public class UserController {
    @Resource
    UserMapper userMapper;
    @PostMapping("save")
    @ApiOperation("记录")
    public Result<?> save(@RequestBody User user){
        userMapper.insert(user);
        return Result.success();
    }
    @GetMapping("find")
    public Result<?> findPage(@RequestParam Integer PN,@RequestParam Integer PS,@RequestParam(required = false) String search){
     Page<User>userPage= userMapper.selectPage(new Page<>(PN, PS), Wrappers.<User>lambdaQuery().like(StringUtils.isNotBlank(search),User::getName,search));
        return Result.success(userPage);
    }
    @PutMapping("update")
    public Result<?>update(@RequestBody User user){
        userMapper.updateById(user);
        return Result.success();
    }
    @DeleteMapping("/{id}")
    public Result<?>delete(@PathVariable Integer id){
        userMapper.deleteById(id);
        return Result.success();
    }
}
